<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="公司名称" prop="factoryName">
        <el-input
          v-model="queryParams.factoryName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料子编号" prop="materialSubcode">
        <el-input
          v-model="queryParams.materialSubcode"
          placeholder="请输入物料子编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验单位名称" prop="inspectionUnitName">
        <el-input
          v-model="queryParams.inspectionUnitName"
          placeholder="请输入检验单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="质检员工号" prop="inspectorEmployeeCode">
        <el-input
          v-model="queryParams.inspectorEmployeeCode"
          placeholder="请输入质检员工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:NcMaterialInspectionRules:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:NcMaterialInspectionRules:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:NcMaterialInspectionRules:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:NcMaterialInspectionRules:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="NcMaterialInspectionRulesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="公司代码" align="center" prop="factoryCode" />
      <el-table-column label="公司名称" align="center" prop="factoryName" />
      <el-table-column label="供应商编码" align="center" prop="supplierCode" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" />
      <el-table-column label="检材类型" align="center" prop="sampleType" />
      <el-table-column label="物料子编号" align="center" prop="materialSubcode" />
      <el-table-column label="物料名称" align="center" prop="materialName" />
      <el-table-column label="物料类型" align="center" prop="materialType" />
      <el-table-column label="物料单位" align="center" prop="materialUnit" />
      <el-table-column label="质检控制" align="center" prop="qualityControl" />
      <el-table-column label="质量状态" align="center" prop="qualityStatus" />
      <el-table-column label="质检节点" align="center" prop="inspectionNode" />
      <el-table-column label="检验项目" align="center" prop="inspectionItem" />
      <el-table-column label="抽检类型" align="center" prop="samplingType" />
      <el-table-column label="检验标准" align="center" prop="inspectionStandard" />
      <el-table-column label="抽样方案" align="center" prop="samplingPlan" />
      <el-table-column label="检验方法" align="center" prop="inspectionMethod" />
      <el-table-column label="取样位置" align="center" prop="samplingPosition" />
      <el-table-column label="合格判定标准" align="center" prop="acceptanceCriteria" />
      <el-table-column label="检验结果" align="center" prop="inspectionResult" />
      <el-table-column label="抽检数比" align="center" prop="samplingRatio" />
      <el-table-column label="破坏数量" align="center" prop="destructiveQuantity" />
      <el-table-column label="承担主体" align="center" prop="responsibleParty" />
      <el-table-column label="检验地点" align="center" prop="inspectionLocation" />
      <el-table-column label="检验单位编码" align="center" prop="inspectionUnitCode" />
      <el-table-column label="检验单位名称" align="center" prop="inspectionUnitName" />
      <el-table-column label="质检员工号" align="center" prop="inspectorEmployeeCode" />
      <el-table-column label="质检员姓名" align="center" prop="inspectorName" />
      <el-table-column label="检具类型" align="center" prop="measuringToolType" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:NcMaterialInspectionRules:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:NcMaterialInspectionRules:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料质检规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="公司代码" prop="factoryCode">
              <el-input v-model="form.factoryCode" placeholder="请输入公司代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="factoryName">
              <el-input v-model="form.factoryName" placeholder="请输入公司名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商编码" prop="supplierCode">
              <el-input v-model="form.supplierCode" placeholder="请输入供应商编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="supplierName">
              <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料子编号" prop="materialSubcode">
              <el-input v-model="form.materialSubcode" placeholder="请输入物料子编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="form.materialName" placeholder="请输入物料名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料单位" prop="materialUnit">
              <el-input v-model="form.materialUnit" placeholder="请输入物料单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="质检控制" prop="qualityControl">
              <el-input v-model="form.qualityControl" placeholder="请输入质检控制" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="质检节点" prop="inspectionNode">
              <el-input v-model="form.inspectionNode" placeholder="请输入质检节点" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验项目" prop="inspectionItem">
              <el-input v-model="form.inspectionItem" placeholder="请输入检验项目" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验标准" prop="inspectionStandard">
              <el-input v-model="form.inspectionStandard" placeholder="请输入检验标准" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="抽样方案" prop="samplingPlan">
              <el-input v-model="form.samplingPlan" placeholder="请输入抽样方案" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验方法" prop="inspectionMethod">
              <el-input v-model="form.inspectionMethod" placeholder="请输入检验方法" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="取样位置" prop="samplingPosition">
              <el-input v-model="form.samplingPosition" placeholder="请输入取样位置" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合格判定标准" prop="acceptanceCriteria">
              <el-input v-model="form.acceptanceCriteria" placeholder="请输入合格判定标准" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验结果" prop="inspectionResult">
              <el-input v-model="form.inspectionResult" placeholder="请输入检验结果" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="抽检数比" prop="samplingRatio">
              <el-input v-model="form.samplingRatio" placeholder="请输入抽检数比" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="破坏数量" prop="destructiveQuantity">
              <el-input v-model="form.destructiveQuantity" placeholder="请输入破坏数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="承担主体" prop="responsibleParty">
              <el-input v-model="form.responsibleParty" placeholder="请输入承担主体" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验地点" prop="inspectionLocation">
              <el-input v-model="form.inspectionLocation" placeholder="请输入检验地点" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验单位编码" prop="inspectionUnitCode">
              <el-input v-model="form.inspectionUnitCode" placeholder="请输入检验单位编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验单位名称" prop="inspectionUnitName">
              <el-input v-model="form.inspectionUnitName" placeholder="请输入检验单位名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="质检员工号" prop="inspectorEmployeeCode">
              <el-input v-model="form.inspectorEmployeeCode" placeholder="请输入质检员工号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="质检员姓名" prop="inspectorName">
              <el-input v-model="form.inspectorName" placeholder="请输入质检员姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
          ref="upload"
          :limit="1"
          accept=".xlsx, .xls"
          :headers="upload.headers"
          :action="upload.url + '?updateSupport=' + upload.updateSupport"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :auto-upload="false"
          drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNcMaterialInspectionRules, getNcMaterialInspectionRules, delNcMaterialInspectionRules, addNcMaterialInspectionRules, updateNcMaterialInspectionRules } from "@/api/business/NcMaterialInspectionRules";
import {getToken} from "@/utils/auth";
export default {
  name: "NcMaterialInspectionRules",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料质检规则表格数据
      NcMaterialInspectionRulesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryName: null,
        supplierName: null,
        sampleType: null,
        materialSubcode: null,
        materialName: null,
        materialType: null,
        samplingType: null,
        inspectionUnitName: null,
        inspectorEmployeeCode: null,
        measuringToolType: null,
      },
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "导入`NcMaterialInspectionRules`信息",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/business/NcMaterialInspectionRules/importData"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料质检规则列表 */
    getList() {
      this.loading = true;
      listNcMaterialInspectionRules(this.queryParams).then(response => {
        this.NcMaterialInspectionRulesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryCode: null,
        factoryName: null,
        supplierCode: null,
        supplierName: null,
        sampleType: null,
        materialSubcode: null,
        materialName: null,
        materialType: null,
        materialUnit: null,
        qualityControl: null,
        qualityStatus: null,
        inspectionNode: null,
        inspectionItem: null,
        samplingType: null,
        inspectionStandard: null,
        samplingPlan: null,
        inspectionMethod: null,
        samplingPosition: null,
        acceptanceCriteria: null,
        inspectionResult: null,
        samplingRatio: null,
        destructiveQuantity: null,
        responsibleParty: null,
        inspectionLocation: null,
        inspectionUnitCode: null,
        inspectionUnitName: null,
        inspectorEmployeeCode: null,
        inspectorName: null,
        measuringToolType: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料质检规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNcMaterialInspectionRules(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料质检规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNcMaterialInspectionRules(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNcMaterialInspectionRules(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据？').then(function() {
        return delNcMaterialInspectionRules(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/NcMaterialInspectionRules/export', {
        ...this.queryParams
      }, `NcMaterialInspectionRules_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "物料质检规则导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('business/NcMaterialInspectionRules/importTemplate', {
      }, `物料质检规则模板.xlsx`)
    },
    /** 文件上传中处理 */
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    /** 文件上传成功处理 */
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    /** 提交上传文件 */
    submitFileForm() {
      this.$refs.upload.submit();
    },
  }
};
</script>
